import {_WpsInvoke,cancelRegWebNotify} from '@/views/office/wps/js/wps.js'
import * as FileApi from '@/api/infra/file'
import {constWpsEnum} from "@/components/WpsOffice/wps/enum";
import {generateUUID} from "@/utils";
const message = useMessage() // 消息弹窗




// 记录当前页面打开的wps
let activeWps = {};
// 异步的队列MAP
let wpsCallBackMap = {};
const getRouteName = function () {
  let hash = window.location.hash;
  if(hash.indexOf("#/redirect") != -1){
    hash = hash.replace("/redirect", "")
  }
  return hash;
}

const handleWpsMessage = function (clientType, name,route,activeWpsId, data) {
  console.log(clientType, name,route,activeWpsId, data,'WPS-handleWpsMessage...clientType, clientType, name,route,activeWpsId, data')

  let func = activeWps[data[constWpsEnum.ActiveWpsId]] ;

  if(data.msgType == "wpsClose"){
    delete activeWps[data[constWpsEnum.ActiveWpsId]] ;
    cancelRegWebNotify(data[constWpsEnum.ActiveWpsId]) ;
  }else if(data.msgType == "SaveDoc"){
    console.log('SaveDoc',data)
    let callWpsTraceId = data[constWpsEnum.CallWpsTraceId]
    if(callWpsTraceId != null ){
      let saveCallBackFuncMap = wpsCallBackMap[callWpsTraceId]
      if(saveCallBackFuncMap != null  && typeof saveCallBackFuncMap.saveCallBackFunc == "function"){
        saveCallBackFuncMap.saveCallBackFunc(data,saveCallBackFuncMap.arguments)
      }
      delete wpsCallBackMap[callWpsTraceId]
    }

  }
  if (typeof func == "function"){
    func(clientType, name,route,activeWpsId, data)
  }else {
    console.log(data,'data')
  }
}

export const onUnmounted = function () {
  console.log('onUnmounted...')
  Object.keys(activeWps).forEach(function(key) {
    cancelRegWebNotify(activeWps[key]);
  });
}

export const newDoc = function (callbackFunc) {
  console.log('newDoc')
  let activeWpsId = "wps-NewDoc-"+generateUUID();
  _WpsInvoke([{
      "NewDoc": {
        [constWpsEnum.ActiveWpsId]:activeWpsId,
      }
    }],
    true,
    false,
    getRouteName(),
    activeWpsId,
    handleWpsMessage
  ) // NewDoc方法对应于OA助手dispatcher支持的方法名
  activeWps[activeWpsId] = callbackFunc;
  return activeWpsId;
}

// 服务器文件地址
// https://code.996code.top:88/yudao-server/admin-api/infra/file/4/get/ba0af12f52535c646d66a1754796400db060f9f7cfa42f5e2d71ccc1ec298dc1.docx
export const openDoc = function (openDocFileId,protectType,protectPwd,callbackFunc) {
  let activeWpsId = "wps-openDoc-"+generateUUID();

  FileApi.getFileById(openDocFileId)
    .then(fileInfo=>{
      console.log('文件信息',fileInfo);
      const  filePathUrl = fileInfo.url;
      const  fileName = fileInfo.name ;
      console.log('openDoc',filePathUrl)
      let openDoc =  {
        "filePathUrl": filePathUrl,
        "fileName": fileName,
        [constWpsEnum.ActiveWpsId]:activeWpsId,
      };
      openDoc["openType"] =  { //文档打开方式
        // 文档保护类型，-1：不启用保护模式，0：只允许对现有内容进行修订，
        // 1：只允许添加批注，2：只允许修改窗体域(禁止拷贝功能)，3：只读
        "protectType": protectType,
        "password": protectPwd
      }
      _WpsInvoke([{
          "OpenDoc":openDoc
        }],
        true,
        false,
        getRouteName(),
        activeWpsId,
        handleWpsMessage
      ) // NewDoc方法对应于OA助手dispatcher支持的方法名
      activeWps[activeWpsId] = callbackFunc;
    })
  return activeWpsId;
}
export const OnlineEditDoc = function (onlineEditDocFileId,callbackFunc) {
  let activeWpsId = "wps-OnlineEditDoc-"+generateUUID();

   FileApi.getFileById(onlineEditDocFileId)
    .then(fileInfo=>{
      console.log('文件信息',fileInfo);
      const  filePathUrl = fileInfo.url;
      const  fileName = fileInfo.name ;
      console.log('OnlineEditDoc',filePathUrl)
      let onlineEditDoc =  {
        "filePathUrl": filePathUrl,
        "fileName": fileName,
        [constWpsEnum.ActiveWpsId]:activeWpsId,
      };

      _WpsInvoke([{
          "OnlineEditDoc": onlineEditDoc
        }],
        true,
        false,
        getRouteName(),
        activeWpsId,
        handleWpsMessage
      ) // NewDoc方法对应于OA助手dispatcher支持的方法名
      activeWps[activeWpsId] = callbackFunc;
    })
  return activeWpsId;
}
export const getDocStatus = function () {
  console.log('getDocStatus')
  _WpsInvoke([{
      "GetDocStatus": {}
    }],
    false)
}

export const SaveDoc = function (activeWpsId, saveCallBackFunc) {
  console.log('SaveDoc')
  let callWpsTraceId = generateUUID();
  _WpsInvoke([{
      "SaveDoc": {
        [constWpsEnum.CallWpsTraceId]:callWpsTraceId ,
        [constWpsEnum.ActiveWpsId]:activeWpsId
      }
    }],
    false,
    false,
    getRouteName(),
    activeWpsId)
    wpsCallBackMap[callWpsTraceId] = {
      saveCallBackFunc:saveCallBackFunc,
      arguments:arguments
    }
}


export const SavePDF = function (activeWpsId, saveCallBackFunc) {
  console.log('SavePDF')
  let callWpsTraceId = generateUUID();
  _WpsInvoke([{
      "SavePDF": {
        [constWpsEnum.CallWpsTraceId]:callWpsTraceId ,
        [constWpsEnum.ActiveWpsId]:activeWpsId
      }
    }],
    false,
    false,
    getRouteName(),
    activeWpsId)
    wpsCallBackMap[callWpsTraceId] = {
      saveCallBackFunc:saveCallBackFunc,
      arguments:arguments
    }
}

export const ExitWPS = function (activeWpsId) {
  console.log('ExitWPS')
  _WpsInvoke([{
    "ExitWPS": {
      [constWpsEnum.ActiveWpsId]:activeWpsId
    }
  }], true,
    false,
    getRouteName(),
    activeWpsId)
}


export const PrintTemplateDoc = function (openDocFileId,callbackFunc,dataInfo) {
  let activeWpsId = "wps-printTemplateDoc-"+generateUUID();

  FileApi.getFileById(openDocFileId)
    .then(fileInfo=>{
      console.log('文件信息',fileInfo);
      const  filePathUrl = fileInfo.url;
      // 打印模板，随机生成一个文件名
      const  fileName = generateUUID()+fileInfo.name ;
      console.log('PrintTemplateDoc',filePathUrl)
      let printTemplateDoc =  {
        "filePathUrl": filePathUrl,
        "fileName": fileName,
        [constWpsEnum.ActiveWpsId]:activeWpsId,
        "dateInfo":dataInfo
      };
      _WpsInvoke([{
          "PrintTemplateDoc":printTemplateDoc
        }],
        false,
        true,
        getRouteName(),
        activeWpsId,
        handleWpsMessage
      ) // NewDoc方法对应于OA助手dispatcher支持的方法名
      activeWps[activeWpsId] = callbackFunc;
    })
  return activeWpsId;
}

